package net.hn.hnms.biz.process.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.process.domain.ShiftTurnoverPo;
import net.hn.hnms.biz.process.domain.vo.ShiftTurnoverExportVo;
import net.hn.hnms.biz.process.domain.vo.ShiftTurnoverVo;
import net.hn.hnms.biz.process.service.ShiftTurnoverService;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.excel.utils.ExcelUtil;
import net.hn.hnms.sys.common.log.annotation.Log;
import net.hn.hnms.sys.common.log.enums.BusinessType;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.mybatis.core.page.TableDataInfo;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import net.hn.hnms.sys.system.api.model.UserVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述: 交接班台账管理
 *
 * @param
 * @auther: WangLong
 * @date: 2022/7/18 13:30
 * @return
 */


@Api(value = "交接班台账管理控制器", tags = {"交接班台账管理"})
@RestController
@RequestMapping("handoverBook")
public class HandoverBookController {
//    新增
//    @SaCheckPermission("process:handoverBook:add")
//    修改
//    @SaCheckPermission("process:handoverBook:edit")
//    删除  刷新
//    @SaCheckPermission("process:handoverBook:remove")
//    列表
//    @SaCheckPermission("process:handoverBook:list")
//    导出
//    @SaCheckPermission("process:handoverBook:export")
//    根据id查询
//    @SaCheckPermission("process:handoverBook:query")
    @Resource
    private ShiftTurnoverService shiftTurnoverService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
//    @SaCheckPermission("process:handoverBook:query")
    @ApiOperation("通过主键查询单条数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "query", dataType = "String")
    })
    @GetMapping("{id}")
    @Log(title = "根据ID查询交接班台账", businessType = BusinessType.OTHER)
    public R selectOne(@PathVariable String id) {
        ShiftTurnoverPo shiftTurnoverPo = shiftTurnoverService.getByIdinfo(id);
        return R.ok(shiftTurnoverPo);
    }

    /**
     * 新增交接班数据
     *
     * @param shiftTurnover 视图对象
     * @return 新增结果
     */
//    @SaCheckPermission("process:handoverBook:add")
    @ApiOperation("新增交接班数据")
    @PostMapping("add")
    @Log(title = "新增交接班台账", businessType = BusinessType.INSERT)
    public R add(@RequestBody ShiftTurnoverPo shiftTurnover) {
        Boolean is = this.shiftTurnoverService.add(shiftTurnover);
        if (is) {
            return R.ok("添加交接班数据");
        }
        return R.fail("添加交接班数据失败");
    }

    /**
     * 修改数据
     *
     * @param shiftTurnover 视图对象
     * @return 新增结果
     */
//    @SaCheckPermission("process:handoverBook:edit")
    @ApiOperation("修改数据")
    @PutMapping("update")
    @Log(title = "修改交接班台账", businessType = BusinessType.UPDATE)
    public R update(@RequestBody ShiftTurnoverPo shiftTurnover) {
        boolean b = this.shiftTurnoverService.updateById(shiftTurnover);
        if (b) {
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
//    @SaCheckPermission("process:handoverBook:remove")
    @ApiImplicitParam(name = "idList", value = "要删除的id值,用逗号间隔", required = true, allowMultiple = true, dataType = "String", paramType = "query")
    @ApiOperation("删除数据")
    @DeleteMapping("delete")
    @Log(title = "删除交接班台账", businessType = BusinessType.DELETE)
    public R del(@RequestBody List<String> idList) {
        Boolean is = this.shiftTurnoverService.delList(idList);
        if (is) {
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }


    /**
     * 功能描述: 条件分页查询交接班台账列表
     *
     * @param pageQuery
     * @param shiftTurnover
     * @return net.hn.hnms.sys.common.mybatis.core.page.TableDataInfo<net.hn.hnms.biz.process.domain.vo.ScglShiftTurnoverVo>
     * @auther: WangLong
     * @date: 2022/7/29 13:48
     */
//    @SaCheckPermission("process:handoverBook:list")
    @ApiOperation("条件分页查询交接班台账列表")
    @GetMapping("pageList")
    @AutoSetMineCode
    @Log(title = "查询交接班台账", businessType = BusinessType.OTHER)
    public R<ResponsePage<ShiftTurnoverPo>> pageList(PageQuery pageQuery, ShiftTurnoverVo shiftTurnover) {
        pageQuery.setPageNum(pageQuery.getPageNum()+1);
        TableDataInfo<ShiftTurnoverPo> page = shiftTurnoverService.pageList(pageQuery, shiftTurnover);
        List<ShiftTurnoverPo> collect = page.getRows().stream().sorted(Comparator.comparing(ShiftTurnoverPo::getHandoverTime).reversed()).collect(Collectors.toList());
        ResponsePage responsePage = new ResponsePage<ShiftTurnoverPo>(collect, page.getTotal());
        return R.ok(responsePage);
    }

    /**
     * 交接班台账导出数据
     * @param response
     * @throws IOException
     */
    @ApiOperation("导出数据")
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody ShiftTurnoverVo shiftTurnover) throws IOException{
        List<ShiftTurnoverExportVo> list = shiftTurnoverService.export(shiftTurnover);
//        List<UserText> list = new ArrayList<>();
//        UserText userText1 = new UserText("小明1","22","122");
//        list.add(userText1);
//        UserText userText2 = new UserText("小明2","22","133");
//        list.add(userText2);
//        UserText userText3 = new UserText("小明3","22","144");
//        list.add(userText3);
//        for (int i = 0; i < list.size(); i++) {
//            SysDept dept = list.get(i).getDept();
//            SysUserExportVo vo = listVo.get(i);
//            if (ObjectUtil.isNotEmpty(dept)) {
//                vo.setDeptName(dept.getDeptName());
//                vo.setLeader(dept.getLeader());
//            }
//        }
        ExcelUtil.exportExcel(list, "交接班台账", ShiftTurnoverExportVo.class, response);

    }
    @ApiOperation("下载导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil.exportExcel(new ArrayList<>(), "交接班台账", ShiftTurnoverExportVo.class,response);
    }

    @ApiOperation("根据ID获取用户信息")
    @PostMapping("/get/{id}")
    @Log(title = "根据ID查询用户信息", businessType = BusinessType.OTHER)
    public R<UserVo> getById(@PathVariable(value = "id")String id)  {
        UserVo userVo= shiftTurnoverService.getById(id);
        return R.ok(userVo);
    }


}
